'use strict';

/**
 * @ngdoc function
 * @name consoleAngularUiApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the consoleAngularUiApp
 */
angular.module('consoleAngularUiApp')
  .controller('servicesCtrl', ['$scope', '$interval', '$servicesServ', function ($scope, $interval, $servicesServ) {

    	$scope.serviceGridOptions = {
    		enableRowSelection: true,
    		enableSelectAll: true,
    		multiSelect: true,
    		enableColumnResizing: true,
    		selectionRowHeaderWidth: 35,
    		rowHeight: 35,
    		showGridFooter: true
    	}

    	$scope.serviceGridOptions.columnDefs = [
    		{
    			name: 'status',
    			cellTemplate: '<div class="glyphicon glyphicon-arrow-up">{{grid.getCellValue(row, col)}}</div>'
    		},
    		{
    			name: 'serviceName',
    			displayName: 'Name'
    		},
    		{
    			name: 'serviceTypeId',
    			displayName: 'Type'
    		},
    		{
    			name: 'port'
    		}
    	]

    	$scope.refreshServicesTableData = function(){
    		$servicesServ.query(function(services){
    			$scope.serviceGridOptions.data = services;
    		})	
    	}

    	$scope.refreshServicesTableData();

    	$scope.refreshTimer = $interval($scope.refreshServicesTableData, 10000);

    	$scope.$on('$destroy', function(){
    		$interval.cancel($scope.refreshTimer);
    	})

  }])


  .controller('servicesNewCtrl', ['$scope', 'serviceTypesVal', '$servicesServ', function($scope, serviceTypesVal, $servicesServ){
  		
  		$scope.serviceTypes = serviceTypesVal.$get();

  		$scope.deploy = function () {
  			if ($scope.serviceForm.$valid) {
  				$servicesServ.deploy($scope.service);
  			}
  		}
  }])

